package org.budo.support.http.client;

import java.io.Serializable;
import java.net.HttpCookie;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

import org.budo.support.lang.util.StringUtil;

/**
 * HTTP响应
 * 
 * @author 明伟
 */
public class Response implements Serializable {
    private static final Charset UTF_8 = Charset.forName("UTF-8");

    private static final long serialVersionUID = 8729025618463464379L;

    private Request request;

    /**
     * 正文类容
     */
    private byte[] responseData;

    /**
     * 正文文本
     */
    private String responseBody;

    /**
     * 包括RequestcCookies和ResponseCookies
     */
    private transient List<HttpCookie> cookies;

    private Map<String, List<String>> headers;

    private Integer status;

    public void setRequest(Request request) {
        this.request = request;
    }

    public Request getRequest() {
        return this.request;
    }

    /**
     * 设置Cookie，在request里有使用
     */
    public void setCookies(List<HttpCookie> cookies) {
        this.cookies = cookies;
    }

    /**
     * 返回Cookie列表
     */
    public List<HttpCookie> getCookies() {
        return this.cookies;
    }

    public HttpCookie getCookie(String name) {
        for (HttpCookie cookie : this.getCookies()) {
            if (cookie.getName().equals(name)) {
                return cookie;
            }
        }
        return null;
    }

    /**
     * 获取response返回的正文类容
     */
    public byte[] getResponseData() {
        return this.responseData;
    }

    public void setResponseData(byte[] responseData) {
        this.responseData = responseData;
    }

    /**
     * 文本形式的response正文
     */
    public String getBody() {
        if (null != this.responseBody) {
            return this.responseBody;
        }

        if (null == this.getResponseData()) {
            return null;
        }

        this.responseBody = new String(this.getResponseData(), UTF_8);
        return this.responseBody;
    }

    public Map<String, List<String>> getHeaders() {
        return this.headers;
    }

    public void setHeaders(Map<String, List<String>> headers) {
        this.headers = headers;
    }

    public String getHeader(String name) {
        if (null == this.getHeaders()) {
            return null;
        }

        List<String> value = this.getHeaders().get(name);
        if (null == value || value.isEmpty()) {
            return null;
        }

        return value.get(0);
    }

    /**
     * getResponseCode
     */
    public Integer getStatus() {
        return this.status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    /**
     * 请用 getBody() 取response内容, 不要用 toString()
     * 
     * @deprecated
     */
    public String toString() {
        if (null != this.getStatus() && this.getStatus().intValue() == 200) {
            return this.getBody();
        }

        return this.toSimpleString();
    }

    public String toSimpleString() {
        return super.toString() + ", status=" + this.getStatus() + ", body=" + StringUtil.cutLeft(this.getBody(), 100) + ", request=" + this.getRequest();
    }
}